/*
 * Copyright (C) 2022 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef DESKTOPDATAHANDLER_H
#define DESKTOPDATAHANDLER_H

#include <QObject>
#include "tabletdesktopbackend.h"
#include "types.h"
#include "categoryitem.h"

class DesktopDataHandler : public QObject
{
    Q_OBJECT
public:
    explicit DesktopDataHandler(QObject *parent = nullptr);
    ~DesktopDataHandler();

    QList<UKUI::ItemId> apps() const;
    QMap<int, UKUI::BaseItem *> items() const;
    QMap<UKUI::BaseItem::Categories, QList<UKUI::ItemId>> categoriesItems() const;

    QString translatedName(const QString &sourceName);

public Q_SLOTS:
    void init();
    void setGroupName(const QString &name,int groupId);
    void updateGroup(int groupId, const QList<QList<int> > &ids);
    void updateHomeScreenPage(const QList<QList<int>> &ids);

    void createGroup(int groupId, const QString &groupName, const QList<int> &itemIdList);
    void removeGroup(quint32 groupId);

private Q_SLOTS:
    void updateDekstopData();
    void addItem(int itemId, int page);
    void addItemIdToTaskbar(int itemId, int insertPosIndex);
    void removeItem(quint32 itemId, int page);
    void iconThemeChanged();
    void onTaskbarAppIdPosExchanged(const QList<int> &appIdListAfterChange);
    void changeAppCategory(const quint32 itemId, const QList<int> newCategory);

    void onItemRemovedFromHomeScreen(int itemId, int page);
    void onItemRemovedFromSet(int itemId, int setId);
    void onAppIdTakedFromTaskbar(int itemId);
    void onAppIdTakedFromSet(int itemId, int setId);
    void onAppIdTakedFromHomeScreen(int itemId, int page);
    void onEduCategoryChanged();

Q_SIGNALS:
    void finished();
    void desktopData(const QMap<int, QList<int>> &pageData,
                     const QMap<int, QList<QList<int>>> &setData,
                     const QMap<int, UKUI::BaseItem *> &itemData);
    void categoriesData(QMap<UKUI::BaseItem::Categories, QList<UKUI::ItemId>> categoriesItems);
    void itemAdded(int itemId, QList<int> categories);
    void appUninstalledFromGroup(int itemId, int group);
    void appUninstalled(int itemId, int page);
    ///
    /// \brief appInstalled 有新的应用安装完成之后发出该信号
    /// \param itemId 新应用的idxia
    /// \param indexOfAllCategory 新应用在‘全部’分类中的索引
    /// \param indexOfCategory 新应用在本身所在分类中的索引
    /// \param page 如果是非大屏项目的话，该参数不能为-1
    /// \param newCategory 是否是新的分类
    /// \param category 新分类
    ///
    void appInstalled(int itemId,
                      int indexOfAllCategory,
                      int indexOfCategory,
                      int page = -1,
                      bool newCategory = false,
                      int category = CategoryItem::All);
    void iconChanged();
    void pageRemoved(int page, int groupId);
    void uninsatllApp(const QString &desktopFile);
    void replaceGroupIdWithAppId(int groupId, int appId);
    void appCategoryChanged(const quint32 itemId, const QList<int> newCategroy);
    void itemStateChanged(int state);
    void homeScreenPageUpdated(const QList<QList<int>> &itemIds);
    void taskbarAppPosExchanged(int fromIndex, int toIndex);
    void itemIdMovedToTaskbar(const int itemId, const int insertPosIndex);
    void itemIdTakedFromTaskbar(const int itemId);
    void itemIdTakedFromDesktop(const int itemId, const int page);
    void itemIdTakedFromGroup(int itemId, int group);

private:
    int groupIdOfItem(int itemId);
    void updateHomeScreenIds(int page, const QList<int> &ids);
    void updateEducategory();

private:
    UKUI::DesktopDataProvider *m_dataBackend;
    QMap<int, QList<int>> m_pageData;
    QMap<int, QList<QList<int>>> m_setData;
    QMap<int, UKUI::BaseItem *> m_itemsData;
    QMap<UKUI::BaseItem::Categories, QList<UKUI::ItemId>> m_categoriesItems;
    QList<UKUI::ItemId> m_apps;
};

#endif // DESKTOPDATAHANDLER_H
